package com.huanglj.himalaya.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huanglj.himalaya.R;
import com.huanglj.himalaya.activitys.DetailActivity;
import com.huanglj.himalaya.adapters.AlbumAdapter;
import com.huanglj.himalaya.base.BaseFragment;
import com.huanglj.himalaya.interfaces.IRecommendCallBack;
import com.huanglj.himalaya.presenters.AlbumDetailPresenter;
import com.huanglj.himalaya.presenters.RecommendPresenter;
import com.huanglj.himalaya.utils.LogUtil;
import com.huanglj.himalaya.views.UILoader;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;


public class RecommendFragment extends BaseFragment implements IRecommendCallBack, AlbumAdapter.OnRecommendItemClickListener {
    private static final String TAG = "RecommendFragment";
    private RecyclerView mRecyclerView;
    private AlbumAdapter mAdapter;
    private RecommendPresenter mRecommendPresenter;
    private UILoader mUIloader;

    @Override
    protected View onSubViewLoaded(final LayoutInflater layoutInflater, final ViewGroup container) {
        mUIloader = new UILoader(getContext()) {
            @Override
            protected View getSuccessView() {
                return createSuccessView(layoutInflater, null);
            }

        };
        //获取逻辑层对象
        mRecommendPresenter = RecommendPresenter.getInstance();
        //设置通知接口的监听
        mRecommendPresenter.registerViewCallBack(this);
        //获取数据
        mRecommendPresenter.getRecommendList();

        //重新更新数据
        mUIloader.setOnRetryClickListener(new UILoader.OnRetryClickListener() {
            @Override
            public void onRetryClick() {
                mRecommendPresenter.getRecommendList();
            }
        });


        //解绑UI
        if (mUIloader.getParent() instanceof ViewGroup) {
            ((ViewGroup) mUIloader.getParent()).removeView(mUIloader);
        }
        return mUIloader;
    }

    @NotNull
    private View createSuccessView(LayoutInflater layoutInflater, ViewGroup container) {
        View view = layoutInflater.inflate(R.layout.fragment_recommend, container, false);
        TwinklingRefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setPureScrollModeOn();
        mRecyclerView = view.findViewById(R.id.recycler_recommend_view);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        //设置成垂直模式
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new AlbumAdapter();
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.bottom= UIUtil.dip2px(view.getContext(),1);
                outRect.top = UIUtil.dip2px(view.getContext(),1);
            }
        });
        mAdapter.setOnRecommendItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
        //设置适配器
        return view;
    }




    @Override
    public void onRecommendListLoaded(List<Album> result) {
        //把数据传给适配器，通知适配器更新UI
        mAdapter.setData(result);
        mUIloader.updateStatus(UILoader.UIStatus.SUCCESS);
    }

    @Override
    public void onNetworkError() {
        //TODO:
        mUIloader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);

    }

    @Override
    public void onEmpty() {
        //TODO:
        mUIloader.updateStatus(UILoader.UIStatus.EMPTY);
    }

    @Override
    public void onLoading() {
        //TODO:
        mUIloader.updateStatus(UILoader.UIStatus.LOADING);
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();

        //取消接口的注册，避免内存泄漏
        if (mRecommendPresenter != null) {
            mRecommendPresenter.unRegisterViewCallBack(this);
            LogUtil.d(TAG, "onDestroy");
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    /**
     * item点击事件
     * @param tag ：被选中的item，从0开始
     * @param album
     */
    @Override
    public void onItemClick(Integer tag, Album album) {
        LogUtil.d(TAG,"tag ; " + tag);
        AlbumDetailPresenter.getInstance().setTargetAlbum(album);
        Intent intent = new Intent(getContext(), DetailActivity.class);
        startActivity(intent);
    }
}
